package com.example.administrator.androidros.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.administrator.androidros.R;
import com.example.administrator.androidros.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by huhaichao on 2018/3/14.
 */

public class AppendDialog extends Dialog {
    @BindView(R.id.view_add_dialog_img_close)
    protected ImageView view_add_dialog_img_close;
    @BindView(R.id.view_add_dialog_tv_id)
    protected TextView view_add_dialog_tv_id;
    @BindView(R.id.view_add_dialog_spinner)
    protected Spinner view_add_dialog_spinner;
    @BindView(R.id.view_add_dialog_text_input_layout)
    protected TextInputLayout view_add_dialog_text_input_layout;
    @BindView(R.id.view_add_dialog_text_input_et_name)
    protected TextInputEditText view_add_dialog_text_input_et_name;
    @BindView(R.id.view_add_dialog_bt_append)
    protected Button view_add_dialog_bt_append;
    @BindView(R.id.view_add_dialog_ll_scene_type)
    protected RelativeLayout view_add_dialog_ll_scene_type;
    private Context mContext;
    private String sceneType=null;//添加的类型
    private OnAddClickListener onAddClickListener;
    private ArrayAdapter adapter;

    public AppendDialog(Context context) {
        super(context, R.style.CustomDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.view_add_dialog, null);
        setContentView(view);
        ButterKnife.bind(this);


        //将可选内容与ArrayAdapter连接起来
        adapter = ArrayAdapter.createFromResource(mContext, R.array.scene_type_name, R.layout.dialog_spinner_text);

        //设置下拉列表的风格
        adapter.setDropDownViewResource(R.layout.dialog_spinner_text);

        //将adapter2 添加到spinner中
        view_add_dialog_spinner.setAdapter(adapter);

        //添加事件Spinner事件监听
        view_add_dialog_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sceneType = StringUtils.getSceneType(view_add_dialog_spinner.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 宽度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    public void setSceneType(String sceneType) {
        this.sceneType = sceneType;
    }


    public void setOnAddClickListener(OnAddClickListener onAddClickListener) {
        this.onAddClickListener = onAddClickListener;
    }

    @Override
    public void show() {
        super.show();

        view_add_dialog_img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        view_add_dialog_bt_append.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sceneName = view_add_dialog_text_input_et_name.getText().toString();
                if (sceneName.equals("")) {
                    view_add_dialog_text_input_layout.setError("名称不能为空");
                } else {
                    view_add_dialog_text_input_layout.setErrorEnabled(false);
                    onAddClickListener.onAddClick(sceneType, sceneName);
                    dismiss();
                }
            }
        });

        setCanceledOnTouchOutside(false);
    }

    public interface OnAddClickListener {
        void onAddClick(String appendType, String name);
    }
}
